package com.bigShen.util;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author : lei.shen
 * @date : 2018/11/29
 */
public class ResourceUtil {

    private static ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();

    public static InputStream getResourceAsStream(String resource) throws IOException {

        InputStream inputStream = classLoaderWrapper.getResourceAsStream(resource, null);
        if (null == inputStream) {
            throw new IOException("Could't find resource " + resource);
        }
        return  inputStream;

    }


}

class TestResource {

    public static void main(String[] args) throws IOException {

        InputStream inputStream = null;
        String resource = "mybatis-config.xml";

        inputStream = TestResource.class.getClassLoader().getResourceAsStream(resource);
        System.out.println("class get inputStream " + inputStream);

        inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
        System.out.println("thread get inputStream " + inputStream);


        inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(resource);
        System.out.println("system get inputStream " + inputStream);

        inputStream = ResourceUtil.getResourceAsStream(resource);
        System.out.println("Util get inputStream " + inputStream);


    }

}